const {join} = require('path')
const { readFile, writeFile } = require('fs')
// 拼接 books.json 绝对路径
const file = (__dirname, 'books.json')

// 1.获取图书
// readFile('books.json', 'utf-8', (err, data) => {
//     if (err) throw err
//     console.log(JSON.parse(data))
// })

// 2.添加图书
readFile('books.json', 'utf-8', (err, data) => {
    if (err) throw err
    let arr = JSON.parse(data)
    arr.push({id:Date.now(), bookname: '美女的自我修养', author: '小鱼卷', publisher: '北京出版社'})
    writeFile(file, JSON.stringify(arr), err => {
        if (err) throw err
        console.log('添加成功')
    })
})